//
//  TZVC1.m
//  OurFramework
//
//  Created by 949699582 on 2019/7/17.
//  Copyright © 2019 com.cunyin.paywaiter. All rights reserved.
//

#import "TZVC1.h"
#import "TZHomeCell.h"
#import "TZHomeViewModel.h"
#import "MJRefresh.h"
#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"
#import "YYCache.h"
static int page = 1;
static const int count = 10;
@interface TZVC1 ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>

@property (nonatomic,strong) TZHomeViewModel *viewModel;

@property (nonatomic,strong) NSArray *bindArr;

@property (nonatomic,strong) NSMutableArray *dataArray; // 数据

@property (nonatomic,strong) UITableView *tableView; // 主tableview

@property (nonatomic,strong) SDCycleScrollView *cycleScrollView;

@end

@implementation TZVC1
{
    NSArray *_imagesURLStrings;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.customNavBar.title =  @"页面1";
    [self.customNavBar wr_setLeftButtonWithImage:[UIImage imageNamed:@"denglu"]];
    [self.customNavBar setOnClickLeftButton:^{
        NSLog(@"点击了导航栏左侧");
    }];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    YYCache *cache = [YYCache cacheWithName:@"ResponseCache1"];
    id response = [cache objectForKey:@"test"];
    
    NSLog(@"response =%@",response);
    
    [self.view addSubview:self.tableView];
    
    _dataArray = [NSMutableArray new];
    [self bindData];
    [self setRefreshFooter];
    
}

- (void)bindData{
    self.viewModel = [TZHomeViewModel new];
    // 建立信号塔
    @weakify(self);
    [self.viewModel.successObject subscribeNext:^(id x) {
        @strongify(self);
        NSArray *arr = x;
        
        NSLog(@"%@",arr);
        self.bindArr = arr;
        [self.dataArray addObjectsFromArray:self.bindArr];
        WeakSelf;
        [weakself.tableView.mj_footer endRefreshing];
        [weakself.tableView reloadData];
    }];
    [self.viewModel.failureObject subscribeNext:^(id x) {
        NSLog(@"数据请求失败: %@",x);
    }];
    
    // 发信号
    [self.viewModel exchangeDataWithPage:page andCount:count];
}

// 添加上拉加载
- (void)setRefreshFooter{
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSLog(@"上拉加载更多内容");
        page++;
        
        [self.viewModel exchangeDataWithPage:page andCount:count];
    }];
    
    ((MJRefreshAutoNormalFooter *)self.tableView.mj_footer).triggerAutomaticallyRefreshPercent = 10;
    ((MJRefreshAutoNormalFooter *)self.tableView.mj_footer).stateLabel.font = [UIFont systemFontOfSize:11];
    //上拉的文字
    [((MJRefreshAutoNormalFooter *)self.tableView.mj_footer) setTitle:@"上拉加载" forState:MJRefreshStateIdle];
    [((MJRefreshAutoNormalFooter *)self.tableView.mj_footer) setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
    [((MJRefreshAutoNormalFooter *)self.tableView.mj_footer) setTitle:@"没有更多了哦" forState:MJRefreshStateNoMoreData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TZHomeCell *cell = [TZHomeCell cellWithTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewScrollPositionNone;
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [TZHomeCell getCellHeight];
}

/**
 *    @brief    懒加载tableView
 */
- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = [UIColor redColor];
        _tableView.frame = CGRectMake(0,[TZCommonClass isXSeries] ? 44 + 44 : 64, SCREEN_WIDTH, [TZCommonClass isXSeries] ? SCREEN_HEIGHT - 88 - 83 : SCREEN_HEIGHT - 49 - 44);
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.cycleScrollView;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        
    }
    return _tableView;
}

- (SDCycleScrollView *)cycleScrollView{
    NSArray *imagesURLStrings = @[
                                  @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",
                                  @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",
                                  @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"
                                  ];
    _imagesURLStrings = imagesURLStrings;
    
    // 情景三：图片配文字
    NSArray *titles = @[@"新建交流QQ群：185534916 ",
                        @"disableScrollGesture可以设置禁止拖动",
                        @"感谢您的支持，如果下载的",
                        @"如果代码在使用过程中出现问题",
                        @"您可以发邮件到gsdios@126.com"
                        ];
    if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _cycleScrollView.titlesGroup = titles;
        _cycleScrollView.currentPageDotColor = [UIColor whiteColor];
        //         --- 模拟加载延迟
        WeakSelf
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           weakself.cycleScrollView.imageURLStringsGroup = imagesURLStrings;
        });
    }
    return _cycleScrollView;
}

@end
